<?php
/**
 * @author liubing
 */
class AlbumsAction extends BaseAction{
	/**
	 * @todo new a albums
	 */
	public function addalbums(){
		if(isset($_GET['id'])){
			$content_id = $_GET['id'];
			$Content = M("Content");
			$Image = M('ContentImage');
			$acontent = $Content->where('content_id='.$content_id)->find();
			$albums = $this->getthumb($acontent['content_text']);
			if($albums != NULL){
				foreach ($albums as $key => $value) {
					$data['image_name'] = $value;
					$data['image_content_id'] = $content_id;
					$isimage = $Image->data($data)->add();
				}
				if($isimage){
					$Content->content_albums=1;
					$Content->where('content_id='.$content_id)->save();
				}
				$this->assign('waitSecond',10);
				$this->assign('jumpUrl','../Content/index');
				$this->success('相册生成成功');
			}else {
				$this->assign('waitSecond',10);
				$this->assign('jumpUrl','../Content/index');
				$this->error('内容中无图片');
			}
		}else{
			$this->assign('waitSecond',10);
			$this->assign('jumpUrl','../Content/index');
			$this->error('无内容选中');
		}
	}
	/**
	 * @to delte a albums
	 */
	public function deletealbums(){
		if(isset($_GET['id'])){
			$content_id = $_GET['id'];
			$Content = M('Content');
			$Albums = M('ContentImage');
			$images = $Albums->where('image_content_id='.$content_id)->select();
			foreach ($images as $key => $file) {
				$filepath = '__APP__/../Public/Uploads/'.$file['image_name'];
				$contentid = $content_id;
				if(file_exists($filepath)){
					if(@unlink($filepath)){
						$Albums->where('image_id='.$file['image_id'])->delete();
						$count = $Albums->where('image_content_id='.$contentid)->count();
						if($count == 0){
							$thiscontent['content_albums']=0;
							$Content->where('content_id='.$contentid)->save($thiscontent);
						}
					}
				}
			}
			$this->assign('waitSecond',10);
			$this->assign('jumpUrl','../Content/index');
			$this->success('成功删除相册');
		}else{
			$this->assign('waitSecond',10);
			$this->assign('jumpUrl','../Content/index');
			$this->error('无内容选中');
		}
	}
	/**
	 * @todo get the thumbimage from content
	 */
	protected function getthumb($sostr){
		$soImages = '~<img [^\>]*\ />~';
		preg_match_all( $soImages, $sostr, $thePics );
		$images = array();
		foreach ($thePics as $key => $value) {
			foreach ($value as $newkey => $newvalue) {
				preg_match_all('~day_\d+/\d+\.(jpg)?(png)?(gif)?(jpeg)?(JPG)?(PNG)?(GIF)?(JPEG)?~',$newvalue,$newtext);
				preg_match_all('~day_\d+/~',$newtext[0][0],$dir);
				preg_match_all('~\d+\.(jpg)?(png)?(gif)?(jpeg)?(JPG)?(PNG)?(GIF)?(JPEG)?~',$newtext[0][0],$name);
				$newimg = '__APP__/../Public/Uploads/'.$dir[0][0]. 'albums_'.$name[0][0];
				$extend = pathinfo($name[0][0]);
				$extend = $extend["extension"];
				$oldimage = '__APP__/../Public/Uploads/'.$newtext[0][0];
				if ($extend == 'jpg' || $extend == "JPG") {
					$img = imagecreatefromjpeg($oldimage);
				}elseif ($extend == 'png' || $extend == "PNG"){
					$img = imagecreatefrompng($oldimage);
				}elseif ($extend == 'gif' || $extend == "GIF"){
					$img = imagecreatefromgif($oldimage);
				}else{
					$img = imagecreatefromwbmp($oldimage);
				}
				$this->ResizeImage($img, 800, 800,$newimg);
				$images[] = $dir[0][0]. 'albums_'.$name[0][0];
			}
			break;
		}
		return $images; // 显示文章中的第一张图片
	}
	/**
	 *
	 * Enter description here ...
	 * @param file $im
	 * @param int $maxwidth
	 * @param int $maxheight
	 * @param String $name
	 */
	protected function ResizeImage($im,$maxwidth,$maxheight,$name){
		//取得当前图片大小
		$width = imagesx($im);
		$height = imagesy($im);
		//生成缩略图的大小
		if(($width > $maxwidth) || ($height > $maxheight)){
			$widthratio = $maxwidth/$width;
			$heightratio = $maxheight/$height;
			if($widthratio < $heightratio){
				$ratio = $widthratio;
			}else{
				$ratio = $heightratio;
			}
			$newwidth = $width * $ratio;
			$newheight = $height * $ratio;

			if(function_exists("imagecopyresampled")){
				$newim = imagecreatetruecolor($newwidth, $newheight);
				imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			}else{
				$newim = imagecreate($newwidth, $newheight);
				imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			}
			ImageJpeg ($newim,$name);
			ImageDestroy ($newim);
		}else{
			ImageJpeg ($im,$name);
		}
	}
}
?>